﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using DAL;
using BLL;

namespace CTRL
{
    public sealed class CtrlBatteryItem
    {
        private static volatile CtrlBatteryItem instance;
        private static object syncRoot = new Object();

        private IDALBatteryItem batteryItemRepository;

        public CtrlBatteryItem()
        {
            this.batteryItemRepository = new DALBatteryItem(new CarBatteryEntities());
        }

        public static CtrlBatteryItem Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (syncRoot)
                    {
                        if (instance == null)
                            instance = new CtrlBatteryItem();
                    }
                }
                return instance;
            }
        }

        public IEnumerable<BatteryItem> GetAllBatteryItems()
        {
            return batteryItemRepository.GetAllItems();
        }

        public IEnumerable<BatteryItem> FindBatteryItemsAtLocation(string location)
        {
            return batteryItemRepository.FindBatteryItemsAtLocation(location);
        }

        public IEnumerable<BatteryItem> FindBatteryItemsAtLocationState(string location, double capacity, BatteryState state)
        {
            return batteryItemRepository.FindBatteryItemsAtLocationState(location, capacity, state);
        }

        public BatteryItem GetBatteryItem(int ID)
        {
            return batteryItemRepository.FindBatteryItem(ID);
        }

        public int UpdateBatteryStates(List<BatteryItem> list, BatteryState state)
        {
            foreach (var item in list)
                batteryItemRepository.UpdateBatteryState(item, state);

            return batteryItemRepository.Save();
        }

        public void UpdateBatteryState(BatteryItem item, BatteryState state)
        {
            batteryItemRepository.UpdateBatteryState(item, state);
            batteryItemRepository.Save();
        }

        public int CreateBatteryItem(int _modelID, string stationName, BatteryState _state)
        {
            CtrlBatteryModel modelCtrl = CtrlBatteryModel.Instance;
            CtrlBatteryStation batStatCtrl = CtrlBatteryStation.Instance;
            BatteryCenter batCenter = batStatCtrl.FindBatteryCenter(stationName);

            if ((modelCtrl.GetBatteryModel(_modelID) != null) && (batCenter != null))
                batteryItemRepository.InsertBatteryItem(new BatteryItem()
                {
                    modelID = _modelID,
                    stationID = batCenter.stationID,
                    state = _state.ToString()
                });

            return batteryItemRepository.Save();
        }

        public int UpdateBatteryItem(int ID, int _modelID, string stationName, BatteryState _state)
        {
            BatteryItem batItem = GetBatteryItem(ID);
            CtrlBatteryModel modelCtrl = CtrlBatteryModel.Instance;
            CtrlBatteryStation batStatCtrl = CtrlBatteryStation.Instance;
            BatteryCenter batCenter = batStatCtrl.FindBatteryCenter(stationName);

            if (batItem != null)
            {
                if ((batCenter != null) && (modelCtrl.GetBatteryModel(_modelID) != null))
                {
                    batItem.modelID = _modelID;
                    batItem.stationID = batCenter.stationID;
                    batItem.state = _state.ToString();
                    batteryItemRepository.UpdateBatteryItem(batItem);
                }
            }
            return batteryItemRepository.Save();
        }

        public int DeleteBatteryItem(int ID)
        {
            BatteryItem batItem = GetBatteryItem(ID);

            if (batItem != null)
                batteryItemRepository.DeleteBatteryItem(batItem.batteryID);

            return batteryItemRepository.Save();
        }
    }
}